<?php
//检测订单，看是否已支付，是否是正常订单

//获取订单信息
// 订单号、订单的标题（团购标题）、订单价格
$orderInfo = array();

$order_sn    = $orderInfo['ordercode'];
$total_price = floatval($orderInfo['price'])*100;
$goods_name  = $orderInfo['title'];

$url_success = 'http://...';		//订单支付的通知页面









//=========步骤1：网页授权获取用户openid============
//通过code获得openid
if (!isset($_GET['code'])){
	//触发微信返回code码
	header('Location: https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx781464faa16830f2&redirect_uri='.urlencode('http://wap.gokks.com/Order/pay.html?id='.$_GET['id'].'&paypath='.$_GET['paypath']).'&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect');
	exit;
}

//使用jsapi接口
include_once('WxPayPubHelper/WxPayPubHelper.php');
$jsApi = new JsApi_pub();

//获取code码，以获取openid
$jsApi->setCode($order_sn);
$openid = $jsApi->getOpenId();
?><DOCTYPE HTML>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <title>微信安全支付</title>
	<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
</head>

<body>
<?=$body?>

<div>立即支付</div><?php
//=========步骤2：使用统一支付接口，获取prepay_id============
//使用统一支付接口
$unifiedOrder = new UnifiedOrder_pub();

$unifiedOrder->setParameter('openid', $openid);			//支付用户Openid
$unifiedOrder->setParameter('body',   $goods_name);		//商品标题
//自定义订单号，此处仅作举例
$unifiedOrder->setParameter('out_trade_no', $order_sn);				//商户订单号
$unifiedOrder->setParameter('total_fee', $total_price);	//总金额
$unifiedOrder->setParameter('notify_url',WxPayConf_pub::NOTIFY_URL);	//通知地址
$unifiedOrder->setParameter('trade_type','JSAPI');						//交易类型

$prepay_id = $unifiedOrder->getPrepayId();
//=========步骤3：使用jsapi调起支付============
$jsApi->setPrepayId($prepay_id);

$jsApiParameters = $jsApi->getParameters();
?>

<script type="text/javascript">
//调用微信JS api 支付
function jsApiCall(){
	WeixinJSBridge.invoke(
		'getBrandWCPayRequest',
		<?php echo $jsApiParameters; ?>,
		function(res){
			WeixinJSBridge.log(res.err_msg);
			if(res.err_msg=='get_brand_wcpay_request:ok'){
				window.location = '<?=$url_success?>'
			}
		}
	);
}
function callpay(){
	if (typeof WeixinJSBridge == "undefined"){
		if( document.addEventListener ){
			document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		}else if (document.attachEvent){
			document.attachEvent('WeixinJSBridgeReady', jsApiCall); 
			document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		}
	}else{
		jsApiCall();
	}
}
</script>
</body>
</html>